package com.example.administrator.szb.http;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.TokenUtil;

import java.util.Iterator;
import java.util.Map;


/**
 * Created by Administrator on 2017/9/10/010.
 */

public class HttpUtil {
    public static void RequestStringGet(RequestQueue queue,int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener){
        String code = url;
        StringRequest stringRequestGet = new StringRequest(method,url,listener,errorListener);
        queue.add(stringRequestGet);
    }

    public static void RequestStringPost(RequestQueue queue, int method, String url, final Map<String,String> map, Response.Listener listener, Response.ErrorListener errorListener){
        StringRequest stringRequestPost = new StringRequest(method,url,listener,errorListener){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        stringRequestPost.setRetryPolicy( new DefaultRetryPolicy( 500000,//默认超时时间，应设置一个稍微大点儿的，例如本处的500000
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,//默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT ) );
        queue.add(stringRequestPost);
    }

    public static void RequestGsonPost(RequestQueue queue, int method,Class clazz, String url, final Map<String,String> map, Response.Listener listener, Response.ErrorListener errorListener){
       if (true){
           String str =  url+"：";

           Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();

           while (entries.hasNext()) {

               Map.Entry<String, String> entry = entries.next();
               str+=entry.getKey()+"="+entry.getValue()+"&";
               //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

           }
           QTLog.e(str);
       }
        GsonRequest gsonRequest = new GsonRequest(method,url,clazz,listener,errorListener){
           @Override
           protected Map<String, String> getParams() throws AuthFailureError {
               return map;
           }
       };
       //最后一个参数，它允许你指定一个退避乘数可以用来实现“指数退避”来从RESTful服务器请求数据。
        gsonRequest.setRetryPolicy(new DefaultRetryPolicy(10*500, 5, 1.2f));
        gsonRequest.setTag("tag");
       queue.add(gsonRequest);
    }

    /**
     * 返回code
     * @return
     */
    public static String getParam(){
        return TokenUtil.md5("szb").substring(0,8);
    }//
}
